<?php

namespace App\Repository\Repositories;

use App\Models\MemberSet;
use App\Models\StoreHead;
use App\Models\StoreOrder;
use App\Models\UserEnter;
use App\Repository\RepositoryInterface\DistributionInterface;
use Illuminate\Support\Facades\DB;

class DistributionRepositories implements DistributionInterface
{
    /**
     * @param $id
     * @param $data
     * @return mixed 修改会员设置
     */
    public function memberSet($id,$data)
    {
        return MemberSet::where('id',$id)->update($data);
    }

    /**
     * @param $id
     * @return mixed 获取会员设置
     */
    public function getSet($id)
    {
        return MemberSet::where('id', $id)->first();
    }
    /**
     * @param $id
     * @return mixed 会员列表
     */
    public function enterList($page)
    {
        return StoreHead::query($page)->paginate(5);
    }
    /**
     * @param $id
     * @return mixed 分销删除
     */
    public function distributionDel($id)
    {
        return StoreHead::where('id',$id)->delete();
    }
    /**
     * @param $id
     * @return mixed 获取分销
     */
    public function getDistribution($id)
    {
        return StoreHead::where('id',$id)->first();
    }
    /**
     * @param $id
     * @return mixed 分销修改
     */
    public function distributionUpdate($data)
    {
        return StoreHead::where('id',$data['id'])->update($data);
    }
    /**
     * @param $id
     * @return mixed 分销添加
     */
    public function distributionAdd($data)
    {
        return StoreHead::create($data);
    }
    /**
     * @param $id
     * @return mixed 记录
     */
    public function getRecord($page)
    {
        return StoreHead::with(['Commission'])->get();
    }
    /**
     * @param $id
     * @return mixed 记录搜索
     */
    public function recordSearch($name)
    {
        $where = [];
        if (!empty($name)){
            $where[] = [
                'name','like','%'.$name.'%'
            ];
        }
        return StoreHead::where($where)->paginate(5);
    }

    /**
     * @param $id
     * @return mixed 营业额统计
     */
    public function statistics()
    {
        $data = StoreOrder::whereIn('status', ['1','2','0'])->get();
        $money = 0;
        foreach($data as $v)
        {
            $money += $v['pay_price'];
        }
        return $money;
    }
    /**
     * @param $id
     * @return mixed 线下额统计
     */
    public function offline()
    {
        $data = StoreOrder::whereIn('status', ['1','2','0'])->where('pay_type',5)->get();
        $money = 0;
        foreach($data as $v)
        {
            $money += $v['pay_price'];
        }
        return $money;
    }
    /**
     * @param $id
     * @return mixed 线下额统计
     */
    public function balance()
    {
        $data = StoreOrder::whereIn('status', ['1','2','0'])->where('pay_type',9)->get();
        $money = 0;
        foreach($data as $v)
        {
            $money += $v['pay_price'];
        }
        return $money;
    }
    /**
     * @param $id
     * @return mixed 线下额统计
     */
    public function goodsPay()
    {
        $data = StoreOrder::whereIn('status', ['1','2','0','3'])->get();
        $money = 0;
        foreach($data as $v)
        {
            $money += $v['pay_price'];
        }
        return $money;
    }
}
